﻿using DevExpress.XtraEditors;
using Shopfloormanagement;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Shopfloormanagement
{
    public partial class Step5 : DevExpress.XtraEditors.XtraForm
    {
        public Step5()
        {
            InitializeComponent();
        }
        int step1;
        int step2;
        int step3;
        int step4;
        int step5;
        int step6;
        int step7;
        int step8;
        int step9;
        private void Word()//加载进度下方文字
        {
            lblStep1.Text = "Problem\nStatement";
            lblStep2.Text = "Problem\nDescription";
            lblStep3.Text = "Interim\nContainment\nactions";
            lblStep4.Text = "Identify\nPossible\nCauses";
            lblStep5.Text = "Test all\nPossible\nCauses";
            lblStep6.Text = "Verify\nPotential\nRoot Cause";
            lblStep7.Text = "Confirmed\nRoot Cause";
            lblStep8.Text = "Permanent\nCorrective\nActions";
            lblStep9.Text = "Further\nActivities";

        }
        private void LoadA3list()//获取上方图片状态
        {

            DataTable A3list = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");

            step1 = (int)A3list.Rows[0][6];
            step2 = (int)A3list.Rows[0][7];
            step3 = (int)A3list.Rows[0][8];
            step4 = (int)A3list.Rows[0][9];
            step5 = (int)A3list.Rows[0][10];
            step6 = (int)A3list.Rows[0][11];
            step7 = (int)A3list.Rows[0][12];
            step8 = (int)A3list.Rows[0][13];
            step9 = (int)A3list.Rows[0][14];
            LoadPicture(step1, step2, step3, step4, step5, step6, step7, step8, step9);
        }
        private void LoadA3step3()//加载信息及判断是否过期
        {
            //获取选中A3的数据
            DataTable A3Step5 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step5", $"A3ReportNumberID = '{rootcause.A3number}'");
            if (A3Step5.Rows.Count == 0) return; // 如果没有数据，直接返回

            DataRow row = A3Step5.Rows[0]; // 取第 0 行数据

            // 加载Cause
            for (int i = 1; i <= 30; i++)
            {
                string columnName = $"Cause{i}";

                // 确保列存在
                if (!A3Step5.Columns.Contains(columnName)) continue;

                // 获取 ComboBox 控件
                Control[] controls = this.Controls.Find($"txt{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.TextEdit combobox)
                {
                    // 赋值，忽略 NULL
                    combobox.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }

            // 加载Direct
            for (int i = 1; i <= 30; i++)
            {
                string columnName = $"Direct{i}";  // 列名
                // 确保列存在
                if (!A3Step5.Columns.Contains(columnName)) continue;

                // 获取 CheckEdit 控件
                Control[] controls = this.Controls.Find($"direct{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.CheckEdit checkEdit)
                {
                    // 将 bit (bool) 赋值给 CheckEdit
                    checkEdit.Checked = row[columnName] != DBNull.Value && Convert.ToBoolean(row[columnName]);
                }
            }
            // 加载Not
            for (int i = 1; i <= 30; i++)
            {
                string columnName = $"Not{i}";  // 列名
                // 确保列存在
                if (!A3Step5.Columns.Contains(columnName)) continue;

                // 获取 CheckEdit 控件
                Control[] controls = this.Controls.Find($"not{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.CheckEdit checkEdit)
                {
                    // 将 bit (bool) 赋值给 CheckEdit
                    checkEdit.Checked = row[columnName] != DBNull.Value && Convert.ToBoolean(row[columnName]);
                }
            }
            // 加载Doubtful
            for (int i = 1; i <= 30; i++)
            {
                string columnName = $"Doubtful{i}";  // 列名
                // 确保列存在
                if (!A3Step5.Columns.Contains(columnName)) continue;

                // 获取 CheckEdit 控件
                Control[] controls = this.Controls.Find($"doubtful{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.CheckEdit checkEdit)
                {
                    // 将 bit (bool) 赋值给 CheckEdit
                    checkEdit.Checked = row[columnName] != DBNull.Value && Convert.ToBoolean(row[columnName]);
                }
            }
            int causeCount = 0;

            for (int i = 1; i <= 30; i++)
            {
                string col = $"Cause{i}";
                if (A3Step5.Columns.Contains(col))
                {
                    string value = row[col]?.ToString().Trim();
                    if (!string.IsNullOrEmpty(value))
                        causeCount++;
                }
            }

            // 显示对应数量的 PanelControl（pal1 - palN）
            for (int i = 1; i <= 30; i++)
            {
                Control[] palCtr = this.Controls.Find($"pal{i}", true);
                if (palCtr.Length > 0 && palCtr[0] is PanelControl pal)
                {
                    pal.Visible = (i <= causeCount);  // causeCount 个显示，其余隐藏
                }
            }
            //获取选中A3Step1的数据(Duedate)
            DataTable A3Step1 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step1", $"A3ReportNumberID = '{rootcause.A3number}'");
            //超期图片变红
            object dbTimeObj = A3Step1.Rows[0]["Duedate"];//获取数据库中的日期数据
            DateTime dbTime;
            if (dbTimeObj != DBNull.Value && DateTime.TryParse(dbTimeObj.ToString(), out dbTime))
            {
                if (dbTime < DateTime.Now)
                {
                    bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step5 = 3", $"ID = '{rootcause.A3number}' and Step5 != 1");
                    //Load A3list 获得step3的值
                    DataTable A3listTemp = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");
                    //获取上方图片状态
                    step1 = (int)A3listTemp.Rows[0][6];
                    step2 = (int)A3listTemp.Rows[0][7];
                    step3 = (int)A3listTemp.Rows[0][8];
                    step4 = (int)A3listTemp.Rows[0][9];
                    step5 = (int)A3listTemp.Rows[0][10];
                    step6 = (int)A3listTemp.Rows[0][11];
                    step7 = (int)A3listTemp.Rows[0][12];
                    step8 = (int)A3listTemp.Rows[0][13];
                    step9 = (int)A3listTemp.Rows[0][14];
                    LoadPicture(step1, step2, step3, step4, step5, step6, step7, step8, step9);
                }
                else
                {
                    return;
                }
            }
        }
        private void LoadPicture(int condition1, int condition2, int condition3, int condition4, int condition5, int condition6, int condition7, int condition8, int condition9)
        {
            switch (condition1)
            {
                case 1:
                    picstep1.Image = Properties.Resources.G1;
                    break;
                case 2:
                    picstep1.Image = Properties.Resources.B1;
                    break;
                case 3:
                    picstep1.Image = Properties.Resources.R1;
                    break;
                case 4:
                    picstep1.Image = Properties.Resources.H1;
                    break;
                default:
                    picstep1.Image = null;
                    break;
            }
            switch (condition2)
            {
                case 1:
                    picstep2.Image = Properties.Resources.G2;
                    break;
                case 2:
                    picstep2.Image = Properties.Resources.B2;
                    break;
                case 3:
                    picstep2.Image = Properties.Resources.R2;
                    break;
                case 4:
                    picstep2.Image = Properties.Resources.H2;
                    break;
                default:
                    picstep2.Image = null;
                    break;
            }
            switch (condition3)
            {
                case 1:
                    picstep3.Image = Properties.Resources.G3;
                    break;
                case 2:
                    picstep3.Image = Properties.Resources.B3;
                    break;
                case 3:
                    picstep3.Image = Properties.Resources.R3;
                    break;
                case 4:
                    picstep3.Image = Properties.Resources.H3;
                    break;
                default:
                    picstep3.Image = null;
                    break;
            }
            switch (condition4)
            {
                case 1:
                    picstep4.Image = Properties.Resources.G4;
                    break;
                case 2:
                    picstep4.Image = Properties.Resources.B4;
                    break;
                case 3:
                    picstep4.Image = Properties.Resources.R4;
                    break;
                case 4:
                    picstep4.Image = Properties.Resources.H4;
                    break;
                default:
                    picstep4.Image = null;
                    break;
            }
            switch (condition5)
            {
                case 1:
                    picstep5.Image = Properties.Resources.G5;
                    break;
                case 2:
                    picstep5.Image = Properties.Resources.B5;
                    break;
                case 3:
                    picstep5.Image = Properties.Resources.R5;
                    break;
                case 4:
                    picstep5.Image = Properties.Resources.H5;
                    break;
                default:
                    picstep5.Image = null;
                    break;
            }
            switch (condition6)
            {
                case 1:
                    picstep6.Image = Properties.Resources.G6;
                    break;
                case 2:
                    picstep6.Image = Properties.Resources.B6;
                    break;
                case 3:
                    picstep6.Image = Properties.Resources.R6;
                    break;
                case 4:
                    picstep6.Image = Properties.Resources.H6;
                    break;
                default:
                    picstep6.Image = null;
                    break;
            }
            switch (condition7)
            {
                case 1:
                    picstep7.Image = Properties.Resources.G7;
                    break;
                case 2:
                    picstep7.Image = Properties.Resources.B7;
                    break;
                case 3:
                    picstep7.Image = Properties.Resources.R7;
                    break;
                case 4:
                    picstep7.Image = Properties.Resources.H7;
                    break;
                default:
                    picstep7.Image = null;
                    break;
            }
            switch (condition8)
            {
                case 1:
                    picstep8.Image = Properties.Resources.G8;
                    break;
                case 2:
                    picstep8.Image = Properties.Resources.B8;
                    break;
                case 3:
                    picstep8.Image = Properties.Resources.R8;
                    break;
                case 4:
                    picstep8.Image = Properties.Resources.H8;
                    break;
                default:
                    picstep8.Image = null;
                    break;
            }
            switch (condition9)
            {
                case 1:
                    picstep9.Image = Properties.Resources.G9;
                    break;
                case 2:
                    picstep9.Image = Properties.Resources.B9;
                    break;
                case 3:
                    picstep9.Image = Properties.Resources.R9;
                    break;
                case 4:
                    picstep9.Image = Properties.Resources.H9;
                    break;
                default:
                    picstep9.Image = null;
                    break;
            }

        }
        private void LoadA3Step4ToUI(DataTable dt)
        {
            if (dt == null || dt.Rows.Count == 0) return;
            DataRow row = dt.Rows[0];

            // 1. 需要读取的全部字段（按顺序）
            string[] columns = new string[]
            {
        "Man1","Man2","Man3","Man4","Man5",
        "Machine1","Machine2","Machine3","Machine4","Machine5",
        "Material1","Material2","Material3","Material4","Material5",
        "Method1","Method2","Method3","Method4","Method5",
        "Mileu1","Mileu2","Mileu3","Mileu4","Mileu5",
        "Measurement1","Measurement2","Measurement3","Measurement4","Measurement5"
            };

            // 2. 收集所有“有值”的项目
            List<string> validItems = new List<string>();
            foreach (var col in columns)
            {
                string value = row[col]?.ToString().Trim();
                if (!string.IsNullOrEmpty(value))
                {
                    validItems.Add(value);
                }
            }

            // 3. 将有值的内容依次填充到 txt1~txt30
            for (int i = 1; i <= 30; i++)
            {
                TextEdit txt = this.Controls.Find($"txt{i}", true).FirstOrDefault() as TextEdit;
                if (txt != null)
                {
                    if (i <= validItems.Count)
                    {
                        txt.Text = validItems[i - 1];
                    }
                    else
                    {
                        txt.Text = ""; // 没有数据的清空
                    }
                }
            }

            // 4. 控制 Panel 显示数量
            for (int i = 1; i <= 30; i++)
            {
                PanelControl pal = this.Controls.Find($"pal{i}", true).FirstOrDefault() as PanelControl;
                if (pal != null)
                {
                    pal.Visible = i <= validItems.Count;
                }
            }
        }

        private void Step5_Load(object sender, EventArgs e)
        {
            DataTable A3Step4 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step4", $"A3ReportNumberID = '{rootcause.A3number}'");
            DataTable A3Step5 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step5", $"A3ReportNumberID = '{rootcause.A3number}'");
            
            Word();
            LoadA3list();
            if (A3Step5.Rows.Count == 0)
            {
                LoadA3Step4ToUI(A3Step4);
            }
            else 
            {
                LoadA3step3();
            }
            
            SetButtonEnable(this);//有调整不保存Next键不显示
            CheckedChanged(sender, e);//绑定CheckEdit如果有变化则Next键不显示
            CheckEdit_Load(sender, e);//绑定CheckEdit互斥事件
            palstep1.BackColor = Color.FromArgb(146, 208, 80);
            palstep2.BackColor = Color.FromArgb(146, 208, 80);
            palstep3.BackColor = Color.FromArgb(146, 208, 80);
            palstep4.BackColor = Color.FromArgb(146, 208, 80);
            palstep5.BackColor = Color.FromArgb(146, 208, 80);


        }
        private void SetButtonEnable(Control control)//检索所有TextEdit控件是否有变化，如有变化则不显示Next
        {

            if (control is TextEdit)
            {
                control.TextChanged += TextEditTextChange;
            }
            else
            {
                var controlCollection = control.Controls;
                foreach (Control subControl in controlCollection)
                {
                    SetButtonEnable(subControl);
                }
            }


        }
        private void CheckedChanged(object sender, EventArgs e)//检索所有CheckEdit控件是否有变化，如有变化则不显示Next
        {
            for (int i = 1; i <= 9; i++)
            {
                // 找到对应的控件
                var direct = this.Controls.Find($"direct{i}", true).FirstOrDefault() as CheckEdit;
                var not = this.Controls.Find($"not{i}", true).FirstOrDefault() as CheckEdit;
                var doubtful = this.Controls.Find($"doubtful{i}", true).FirstOrDefault() as CheckEdit;

                // 绑定事件
                if (direct != null) direct.CheckedChanged += CheckEdit_CheckedChanged2;
                if (not != null) not.CheckedChanged += CheckEdit_CheckedChanged2;
                if (doubtful != null) doubtful.CheckedChanged += CheckEdit_CheckedChanged2;
            }
        }
        private void CheckEdit_CheckedChanged2(object sender, EventArgs e)
        {
            CheckEdit changedCheck = sender as CheckEdit;

            if (changedCheck != null)
            {
                TextEditTextChange(sender, e);
            }
        }//检索所有CheckEdit控件是否有变化，如有变化则不显示Next
        private void TextEditTextChange(object sender, EventArgs e)
        {

            btnNext.Visible = false;

        }//为SetButtonEnable制作的命令
        private void btnNext_Click(object sender, EventArgs e)
        {
            // 检查是否有未判断的选项或判断异常
            for (int i = 1; i <= 30; i++)
            {
                // 获取当前的 ComboBox 和 CheckEdit 控件
                TextEdit cob = (TextEdit)this.Controls.Find($"txt{i}", true).FirstOrDefault();
                CheckEdit direct = (CheckEdit)this.Controls.Find($"direct{i}", true).FirstOrDefault();
                CheckEdit not = (CheckEdit)this.Controls.Find($"not{i}", true).FirstOrDefault();
                CheckEdit doubtful = (CheckEdit)this.Controls.Find($"doubtful{i}", true).FirstOrDefault();

                // 如果 ComboBox 有选项且 CheckEdit 控件都存在
                if (cob != null && !string.IsNullOrWhiteSpace(cob.Text) && direct != null && not != null && doubtful != null)
                {
                    int trueCount = (direct.Checked ? 1 : 0) + (not.Checked ? 1 : 0) + (doubtful.Checked ? 1 : 0);

                    // 如果三个 CheckEdit 都未选中
                    if (trueCount == 0)
                    {
                        MessageBox.Show($"存在可能原因未判断", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return; // 提示后结束
                    }
                    // 如果有两个或三个 CheckEdit 为选中状态，提示判断异常
                    else if (trueCount > 1)
                    {
                        MessageBox.Show($"判断异常，不能选择多个判断项", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return; // 提示后结束
                    }
                }
            }
            bool allDirectFalse = true;
            for (int i = 1; i <= 30; i++)
            {
                CheckEdit direct = (CheckEdit)this.Controls.Find($"direct{i}", true).FirstOrDefault();
                if (direct != null && direct.Checked)
                {
                    allDirectFalse = false;
                    break;
                }
            }

            if (allDirectFalse)
            {
                MessageBox.Show("至少选中一个Direct Cause", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step5 = 1", $"ID = '{rootcause.A3number}'");
                if (reslist == true)
                {
                    Step5fish frm = new Step5fish();
                    frm.Show();
                    this.Hide();
                }
                
            }
            
           
        }
        //CheckEdit互斥事件
        private void CheckEdit_CheckedChanged(object sender, EventArgs e)
        {
            CheckEdit current = sender as CheckEdit;
            if (current == null || !current.Checked) return; // 只处理当前为True的情况

            for (int i = 1; i <= 30; i++)
            {
                CheckEdit direct = this.Controls.Find($"direct{i}", true).FirstOrDefault() as CheckEdit;
                CheckEdit not = this.Controls.Find($"not{i}", true).FirstOrDefault() as CheckEdit;
                CheckEdit doubtful = this.Controls.Find($"doubtful{i}", true).FirstOrDefault() as CheckEdit;

                if (current == direct)
                {
                    if (not != null) not.Checked = false;
                    if (doubtful != null) doubtful.Checked = false;
                    break;
                }
                else if (current == not)
                {
                    if (direct != null) direct.Checked = false;
                    if (doubtful != null) doubtful.Checked = false;
                    break;
                }
                else if (current == doubtful)
                {
                    if (direct != null) direct.Checked = false;
                    if (not != null) not.Checked = false;
                    break;
                }
            }
        }
        //绑定CheckEdit互斥事件
        private void CheckEdit_Load(object sender, EventArgs e)
        {
            for (int i = 1; i <= 30; i++)
            {
                var direct = this.Controls.Find($"direct{i}", true).FirstOrDefault() as CheckEdit;
                var not = this.Controls.Find($"not{i}", true).FirstOrDefault() as CheckEdit;
                var doubtful = this.Controls.Find($"doubtful{i}", true).FirstOrDefault() as CheckEdit;

                if (direct != null) direct.CheckedChanged += CheckEdit_CheckedChanged;
                if (not != null) not.CheckedChanged += CheckEdit_CheckedChanged;
                if (doubtful != null) doubtful.CheckedChanged += CheckEdit_CheckedChanged;
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {

            Step4 frm = new Step4();
            frm.Show();
            this.Hide();

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataTable A3Step5 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step5", $"A3ReportNumberID = '{rootcause.A3number}'");
            if (A3Step5.Rows.Count > 0)
            {

                string content = $@"Cause1 = '{txt1.Text}',Cause2 = '{txt2.Text}',Cause3 = '{txt3.Text}',Cause4 = '{txt4.Text}',Cause5 = '{txt5.Text}',Cause6 = '{txt6.Text}',Cause7 = '{txt7.Text}',Cause8 = '{txt8.Text}',Cause9 = '{txt9.Text}',Cause10 = '{txt10.Text}',
                    Cause11 = '{txt11.Text}',Cause12 = '{txt12.Text}',Cause13 = '{txt13.Text}',Cause14 = '{txt14.Text}',Cause15 = '{txt15.Text}',Cause16 = '{txt16.Text}',Cause17 = '{txt17.Text}',Cause18 = '{txt18.Text}',Cause19 = '{txt19.Text}',Cause20 = '{txt20.Text}',
                    Cause21 = '{txt21.Text}',Cause22 = '{txt22.Text}',Cause23 = '{txt23.Text}',Cause24 = '{txt24.Text}',Cause25 = '{txt25.Text}',Cause26 = '{txt26.Text}',Cause27 = '{txt27.Text}',Cause28 = '{txt28.Text}',Cause29 = '{txt29.Text}',Cause30 = '{txt30.Text}',
                    Direct1 = '{direct1.Checked}',Direct2 = '{direct2.Checked}',Direct3 = '{direct3.Checked}',Direct4 = '{direct4.Checked}',Direct5 = '{direct5.Checked}',Direct6 = '{direct6.Checked}',Direct7 = '{direct7.Checked}',Direct8 = '{direct8.Checked}',Direct9 = '{direct9.Checked}',Direct10 = '{direct10.Checked}',
                    Direct11 = '{direct11.Checked}',Direct12 = '{direct12.Checked}',Direct13 = '{direct13.Checked}',Direct14 = '{direct14.Checked}',Direct15 = '{direct15.Checked}',Direct16 = '{direct16.Checked}',Direct17 = '{direct17.Checked}',Direct18 = '{direct18.Checked}',Direct19 = '{direct19.Checked}',Direct20 = '{direct20.Checked}',
                    Direct21 = '{direct21.Checked}',Direct22 = '{direct22.Checked}',Direct23 = '{direct23.Checked}',Direct24 = '{direct24.Checked}',Direct25 = '{direct25.Checked}',Direct26 = '{direct26.Checked}',Direct27 = '{direct27.Checked}',Direct28 = '{direct28.Checked}',Direct29 = '{direct29.Checked}',Direct30 = '{direct30.Checked}',
                    Not1 = '{not1.Checked}',Not2 = '{not2.Checked}',Not3 = '{not3.Checked}',Not4 = '{not4.Checked}',Not5 = '{not5.Checked}',Not6 = '{not6.Checked}',Not7 = '{not7.Checked}',Not8 = '{not8.Checked}',Not9 = '{not9.Checked}',Not10 = '{not10.Checked}',
                    Not11 = '{not11.Checked}',Not12 = '{not12.Checked}',Not13 = '{not13.Checked}',Not14 = '{not14.Checked}',Not15 = '{not15.Checked}',Not16 = '{not16.Checked}',Not17 = '{not17.Checked}',Not18 = '{not18.Checked}',Not19 = '{not19.Checked}',Not20 = '{not20.Checked}',
                    Not21 = '{not21.Checked}',Not22 = '{not22.Checked}',Not23 = '{not23.Checked}',Not24 = '{not24.Checked}',Not25 = '{not25.Checked}',Not26 = '{not26.Checked}',Not27 = '{not27.Checked}',Not28 = '{not28.Checked}',Not29 = '{not29.Checked}',Not30 = '{not30.Checked}',
                    Doubtful1 = '{doubtful1.Checked}',Doubtful2 = '{doubtful2.Checked}',Doubtful3 = '{doubtful3.Checked}',Doubtful4 = '{doubtful4.Checked}',Doubtful5 = '{doubtful5.Checked}',Doubtful6 = '{doubtful6.Checked}',Doubtful7 = '{doubtful7.Checked}',Doubtful8 = '{doubtful8.Checked}',Doubtful9 = '{doubtful9.Checked}',Doubtful10 = '{doubtful10.Checked}',
                    Doubtful11 = '{doubtful11.Checked}',Doubtful12 = '{doubtful12.Checked}',Doubtful13 = '{doubtful13.Checked}',Doubtful14 = '{doubtful14.Checked}',Doubtful15 = '{doubtful15.Checked}',Doubtful16 = '{doubtful16.Checked}',Doubtful17 = '{doubtful17.Checked}',Doubtful18 = '{doubtful18.Checked}',Doubtful19 = '{doubtful19.Checked}',Doubtful20 = '{doubtful20.Checked}',
                    Doubtful21 = '{doubtful21.Checked}',Doubtful22 = '{doubtful22.Checked}',Doubtful23 = '{doubtful23.Checked}',Doubtful24 = '{doubtful24.Checked}',Doubtful25 = '{doubtful25.Checked}',Doubtful26 = '{doubtful26.Checked}',Doubtful27 = '{doubtful27.Checked}',Doubtful28 = '{doubtful28.Checked}',Doubtful29 = '{doubtful29.Checked}',Doubtful30 = '{doubtful30.Checked}'";
                bool resstep5 = LSQL.SQL.Update(SystemInfo.SQL, "A3_Step5", content, $"A3ReportNumberID = '{rootcause.A3number}'");
                bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step5 = 2", $"ID = '{rootcause.A3number}'");
                if (resstep5 == true && reslist == true)
                {

                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Step5_Load(sender, e);
                    btnNext.Visible = true;
                }
                else
                {
                    MessageBox.Show("更新失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }

            }

            else
            {
                string colunm = "[A3ReportNumberID],[Cause1],[Cause2],[Cause3],[Cause4],[Cause5],[Cause6],[Cause7],[Cause8],[Cause9],[Cause10]," +
                    "[Cause11],[Cause12],[Cause13],[Cause14],[Cause15],[Cause16],[Cause17],[Cause18],[Cause19],[Cause20]," +
                    "[Cause21],[Cause22],[Cause23],[Cause24],[Cause25],[Cause26],[Cause27],[Cause28],[Cause29],[Cause30]," +
                    "[Direct1],[Direct2],[Direct3],[Direct4],[Direct5],[Direct6],[Direct7],[Direct8],[Direct9],[Direct10]," +
                    "[Direct11],[Direct12],[Direct13],[Direct14],[Direct15],[Direct16],[Direct17],[Direct18],[Direct19],[Direct20]," +
                    "[Direct21],[Direct22],[Direct23],[Direct24],[Direct25],[Direct26],[Direct27],[Direct28],[Direct29],[Direct30]," +
                    "[Not1],[Not2],[Not3],[Not4],[Not5],[Not6],[Not7],[Not8],[Not9],[Not10]," +
                    "[Not11],[Not12],[Not13],[Not14],[Not15],[Not16],[Not17],[Not18],[Not19],[Not20]," +
                    "[Not21],[Not22],[Not23],[Not24],[Not25],[Not26],[Not27],[Not28],[Not29],[Not30]," +
                    "[Doubtful1],[Doubtful2],[Doubtful3],[Doubtful4],[Doubtful5],[Doubtful6],[Doubtful7],[Doubtful8],[Doubtful9],[Doubtful10],"+
                    "[Doubtful11],[Doubtful12],[Doubtful13],[Doubtful14],[Doubtful15],[Doubtful16],[Doubtful17],[Doubtful18],[Doubtful19],[Doubtful20]," +
                    "[Doubtful21],[Doubtful22],[Doubtful23],[Doubtful24],[Doubtful25],[Doubtful26],[Doubtful27],[Doubtful28],[Doubtful29],[Doubtful30]";
                string content = $@"'{rootcause.A3number}','{txt1.Text}','{txt2.Text}','{txt3.Text}','{txt4.Text}','{txt5.Text}','{txt6.Text}','{txt7.Text}','{txt8.Text}','{txt9.Text}','{txt10.Text}',
                    '{txt11.Text}','{txt12.Text}','{txt13.Text}','{txt14.Text}','{txt15.Text}','{txt16.Text}','{txt17.Text}','{txt18.Text}','{txt19.Text}','{txt20.Text}',
                    '{txt21.Text}','{txt22.Text}','{txt23.Text}','{txt24.Text}','{txt25.Text}','{txt26.Text}','{txt27.Text}','{txt28.Text}','{txt29.Text}','{txt30.Text}',
                    '{direct1.Checked}','{direct2.Checked}','{direct3.Checked}','{direct4.Checked}','{direct5.Checked}','{direct6.Checked}','{direct7.Checked}','{direct8.Checked}','{direct9.Checked}','{direct10.Checked}',
                    '{direct11.Checked}','{direct12.Checked}','{direct13.Checked}','{direct14.Checked}','{direct15.Checked}','{direct16.Checked}','{direct17.Checked}','{direct18.Checked}','{direct19.Checked}','{direct20.Checked}',
                    '{direct21.Checked}','{direct22.Checked}','{direct23.Checked}','{direct24.Checked}','{direct25.Checked}','{direct26.Checked}','{direct27.Checked}','{direct28.Checked}','{direct29.Checked}','{direct30.Checked}',
                    '{not1.Checked}','{not2.Checked}','{not3.Checked}','{not4.Checked}','{not5.Checked}','{not6.Checked}','{not7.Checked}','{not8.Checked}','{not9.Checked}','{not10.Checked}',
                    '{not11.Checked}','{not12.Checked}','{not13.Checked}','{not14.Checked}','{not15.Checked}','{not16.Checked}','{not17.Checked}','{not18.Checked}','{not19.Checked}','{not20.Checked}',
                    '{not21.Checked}','{not22.Checked}','{not23.Checked}','{not24.Checked}','{not25.Checked}','{not26.Checked}','{not27.Checked}','{not28.Checked}','{not29.Checked}','{not30.Checked}',
                    '{doubtful1.Checked}','{doubtful2.Checked}','{doubtful3.Checked}','{doubtful4.Checked}','{doubtful5.Checked}','{doubtful6.Checked}','{doubtful7.Checked}','{doubtful8.Checked}','{doubtful9.Checked}','{doubtful10.Checked}',
                    '{doubtful11.Checked}','{doubtful12.Checked}','{doubtful13.Checked}','{doubtful14.Checked}','{doubtful15.Checked}','{doubtful16.Checked}','{doubtful17.Checked}','{doubtful18.Checked}','{doubtful19.Checked}','{doubtful20.Checked}',
                    '{doubtful21.Checked}','{doubtful22.Checked}','{doubtful23.Checked}','{doubtful24.Checked}','{doubtful25.Checked}','{doubtful26.Checked}','{doubtful27.Checked}','{doubtful28.Checked}','{doubtful29.Checked}','{doubtful30.Checked}'";
                int res = LSQL.SQL.Insert(SystemInfo.SQL, "A3_Step5", colunm, content);
                if (res == 1)
                {
                    bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step5 = 2", $"ID = '{rootcause.A3number}'");
                    if (reslist == true)
                    {
                        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Step5_Load(sender, e);
                        btnNext.Visible = true;
                    }
                }
                else
                {
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }

            }
        }

        private void btnClosed_Click(object sender, EventArgs e)
        {
            // 弹出确认对话框，询问是否关闭
            DialogResult result = MessageBox.Show("是否确定关闭？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                // 用户选择“是”，执行关闭操作
                this.Close(); // 关闭当前窗体
            }
            else
            {
                // 用户选择“否”，返回，不做任何操作
                return;
            }
        }
        //点击图片跳转功能
        private void picstep1_Click(object sender, EventArgs e)
        {
            if (step1 == 4)
            {
                return;
            }
            else
            {
                Step1 frm = new Step1();
                this.Close();
                frm.Show();
            }
        }

        private void picstep2_Click(object sender, EventArgs e)
        {
            if (step2 == 4)
            {
                return;
            }
            else
            {
                Step2 frm = new Step2();
                this.Close();
                frm.Show();
            }
        }

        private void picstep3_Click(object sender, EventArgs e)
        {
            if (step3 == 4)
            {
                return;
            }
            else
            {
                Step3 frm = new Step3();
                this.Close();
                frm.Show();
            }
        }
        private void picstep4_Click(object sender, EventArgs e)
        {
            if (step4 == 4)
            {
                return;
            }
            else
            {
                Step4 frm = new Step4();
                this.Close();
                frm.Show();
            }
        }

        private void picstep5_Click(object sender, EventArgs e)
        {
            if (step5 == 4)
            {
                return;
            }
            else
            {
                Step5 frm = new Step5();
                this.Close();
                frm.Show();
            }
        }

        private void picstep6_Click(object sender, EventArgs e)
        {
            if (step6 == 4)
            {
                return;
            }
            else
            {
                Step6 frm = new Step6();
                this.Close();
                frm.Show();
            }
        }

        private void picstep7_Click(object sender, EventArgs e)
        {
            if (step7 == 4)
            {
                return;
            }
            else
            {
                Step7 frm = new Step7();
                this.Close();
                frm.Show();
            }
        }

        private void picstep8_Click(object sender, EventArgs e)
        {
            if (step8 == 4)
            {
                return;
            }
            else
            {
                Step8 frm = new Step8();
                this.Close();
                frm.Show();
            }
        }

        private void picstep9_Click(object sender, EventArgs e)
        {
            if (step9 == 4)
            {
                return;
            }
            else
            {
                Step9 frm = new Step9();
                this.Close();
                frm.Show();
            }
        }
    }
}